package edu.del.springbootredisbusinesspractice.commons.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class RabbitMQConfig {
    // 延迟队列
    public static final String ORDER_DELAY_QUEUE = "order-delay-queue";
    // 延迟路由键
    public static final String ORDER_DELAY_ROUTING_KEY = "order.delay.routing.key";

    // 订单交换机
    public static final String ORDER_EXCHANGE = "order-exchange";
    // 订单队列
    public static final String ORDER_QUEUE = "order-queue";
    // 路由键
    public static final String ORDER_ROUTING_KEY = "order.routing.key";

    // 支付成功队列
    public static final String ORDER_PAID_QUEUE = "order.paid.queue";
    // 支付成功路由键
    public static final String ORDER_PAID_ROUTING_KEY = "order.paid.routing.key";

    // 取消支付队列
    public static final String ORDER_CANCEL_QUEUE = "order.cancel.queue";
    // 取消支付路由键
    public static final String ORDER_CANCEL_ROUTING_KEY = "order.cancel.routing.key";



    // 死信交换机
    public static final String DEAD_LETTER_EXCHANGE = "dead-letter-exchange";
    // 死信队列
    public static final String DEAD_LETTER_QUEUE = "order-dlx";
    // 死信路由键
    public static final String DEAD_LETTER_ROUTING_KEY = "order.dlx.routing.key";




    @Bean
    public DirectExchange  directExchange() {
        // 设置 Exchange 的持久化属性为 true 不自动删除
        return ExchangeBuilder.directExchange(ORDER_EXCHANGE).durable(true).build();
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        // 设置 Exchange 的持久化属性为 true 不自动删除
        return ExchangeBuilder.directExchange(DEAD_LETTER_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue orderQueue() {
        // 持久化队列
        return new Queue(ORDER_QUEUE, true, false, false);
    }

    @Bean
    public Queue deadLetterQueue() {
        // 持久化队列
        return new Queue(DEAD_LETTER_QUEUE, true, false, false);
    }

    @Bean
    public Queue orderDelayQueue() {
        // 设置死信交换机和路由键, 消息过期后进入死信队列
        Map<String, Object> args = Map.of(
                "x-dead-letter-exchange", DEAD_LETTER_EXCHANGE,
                "x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY
        );
        return new Queue(ORDER_DELAY_QUEUE, true, false, false, args);
    }

    @Bean
    public Queue orderPaidQueue() {
        // 持久化队列
        return new Queue(ORDER_PAID_QUEUE, true, false, false);
    }

    @Bean
    public Queue orderCancelQueue() {
        // 持久化队列
        return new Queue(ORDER_CANCEL_QUEUE, true, false, false);
    }


    @Bean
    public Binding orderBinding() {
        return BindingBuilder
                .bind(orderQueue())
                .to(directExchange())
                .with(ORDER_ROUTING_KEY);
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder
                .bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with(DEAD_LETTER_ROUTING_KEY);
    }

    @Bean
    public Binding orderDelayBinding() {
        return BindingBuilder
                .bind(orderDelayQueue())
                .to(directExchange())
                .with(ORDER_DELAY_ROUTING_KEY);
    }

    @Bean
    public Binding orderPaidBinding() {
        return BindingBuilder
                .bind(orderPaidQueue())
                .to(directExchange())
                .with(ORDER_PAID_ROUTING_KEY);
    }

    @Bean
    public Binding orderCancelBinding() {
        return BindingBuilder
                .bind(orderCancelQueue())
                .to(directExchange())
                .with(ORDER_CANCEL_ROUTING_KEY);
    }
}
